package org.example.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.system.entity.Unit;
import org.example.system.mapper.UnitMapper;
import org.example.system.service.UnitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;

/**
 * 单元服务层实现类
 */
@Service
public class UnitServiceImpl implements UnitService {
    @Autowired
    private UnitMapper unitMapper;

    /**
     * 按条件分页查询
     * @param unit
     * @return
     */
    @Override
    public IPage<Unit> selectPage(Unit unit){
        LambdaQueryWrapper<Unit> wrapper = new LambdaQueryWrapper<>();
        //条件
        if (unit.getId() != null){
            wrapper.eq(Unit:: getId,unit.getId());
        }
        IPage<Unit> page = new Page<>();
        page.setCurrent(unit.getPageNum());
        page.setSize(unit.getPageSize());
        return unitMapper.selectPage(page,wrapper);
    }

    /**
     * 添加单元
     * @param unit
     * @return
     */
    @Override
    public int add(Unit unit) {
        LambdaQueryWrapper<Unit> wrapper = new LambdaQueryWrapper<>();
        unit.setId(unitMapper.selectCount(wrapper)+1);
        unit.setCreateTime(new Date());
        return unitMapper.insert(unit);
    }

    /**
     * 修改
     * @param unit
     * @return
     */
    @Override
    public int update(Unit unit) {
        unit.setUpdateTime(new Date());
        return unitMapper.updateById(unit);
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @Override
    public int deleteBatch(Long[] ids) {
        return unitMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 根据id获取信息
     * @param id
     * @return
     */
    @Override
    public Unit getOne(Long id) {
        return unitMapper.selectById(id);
    }
}
